package com.mp3s.plugin_quote.qday.utils;


import java.util.List;

import com.mp3s.interface3.IDataSave;
import com.mp3s.interface3.ISchedule;
import com.mp3s.interface3.QuoteData;
import com.mp3s.interface3.TradeTime;
import com.mp3s.plugin_quote.Download;

import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;

public class schedule implements ISchedule{

    private String gTaskId = "sh000000";           //stock id
    private String gCron = "*/30 * * * * ?";     //10秒
    
    public void CronTask(String taskId, String cronExpression,IDataSave isave) {
        CronUtil.schedule(taskId, cronExpression, new Task() {
            @Override
            public void execute() {
                // 执行任务逻辑
                // System.out.println(""+DateTime.now()+","+taskId);
                //在合规时间内，进行下载
                if(false == TradeTime.isTradeTime()){
                    return;
                }

                String out = Download.down(taskId);
                List<QuoteData> q = utils.parseSinaQuote(out);
                isave.save(q);
            }
        });
    }

    @Override
    public void init(String taskId,String cron,IDataSave isave) {
        // TODO Auto-generated method stub
        CronUtil.setMatchSecond(true);
        if(cron.length() < 1){
            cron = gCron;
        }
        if(taskId.length() < 1){
            taskId = gTaskId;
        }
        CronTask(taskId,cron,isave);
        // throw new UnsupportedOperationException("Unimplemented method 'init'");
    }

    @Override
    public void start() {
        // TODO Auto-generated method stub
        CronUtil.start();
        // throw new UnsupportedOperationException("Unimplemented method 'start'");
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub
        // throw new UnsupportedOperationException("Unimplemented method 'stop'");
        CronUtil.stop();
    }

    @Override
    public void init(List<String> taskIdList, String cron, IDataSave isave) {
        // TODO Auto-generated method stub
        // throw new UnsupportedOperationException("Unimplemented method 'init'");
    }
}
